home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-08-01 | 10.0 KB | 424 lines |
- /*
- * Grid.java - The game area grid
- * Copyright (C) 2000 Romain Guy
- * guy.romain@bigfoot.com
- * www.jext.org
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
- import waba.fx.*;
- import waba.util.*;
- import waba.sys.*;
-
- /**
- * The grid object represents the game area. It draws a grid
- * which amount of cells can be specified. The grid also holds
- * and handle the solutions of the puzzle.
- * @author Romain Guy <guy.romain@bigfoot.com>
- * @version 1.1
- */
-
- public class Grid
- {
- // defines the width in pixels of a single cell
- public static final int CELL_WIDTH = 5;
-
- // strings containing infos
- private MiniString[] rowsInfos;
- private MiniString[] colsInfos;
-
- // high score of current level
- private byte[] highScore;
- // the level name
- private String levelName;
- // the level
- private byte[] level;
- // the level viewed by the user
- private byte[] userLevel;
- // 'to be drawn' cells amount
- private int cellsToDraw;
- // number of cells found
- private int cellsFound;
- // number of cells to find
- private int hiddenCells;
- // defines the amount of cells in a row/column
- private int cells;
- // controls like fields
- private int width, height, x, y;
- // the drawing area
- private Graphics g;
- // parent
- private eCross parent;
-
- /**
- * Creates a new grid containing 15 rows and 15
- * columns.
- * @param x The x origin
- * @param y The y origin
- * @param parent The parent of the grid
- */
-
- public Grid(int x, int y, eCross parent)
- {
- this(x, y, 15, parent);
- }
-
- /**
- * Creates a new grid.
- * @param x The x origin
- * @param y The y origin
- * @param cells The amount of cells in each row/column
- * @param parent The parent of the grid
- */
-
- public Grid(int x, int y, int cells, eCross parent)
- {
- int wh = cells * CELL_WIDTH + (cells + 1);
- this.x = x;
- this.y = y;
- this.width = wh;
- this.height = wh;
- this.cells = cells;
- this.parent = parent;
- this.g = parent.getBackBuffer();
- }
-
- /**
- * Sets the current level.
- * @param level The level stored in a byte array
- */
-
- public void setLevel(Level level)
- {
- this.level = level.getLevel();
- this.levelName = level.getName();
- this.highScore = level.getHighScore();
- level = null;
-
- reset();
-
- hiddenCells = 0;
- buildStrings();
- }
-
- /**
- * Checks if given time beats current high score.
- */
-
- public boolean isHighScore(byte minutes, byte seconds)
- {
- if (minutes > highScore[0])
- return true;
- else if (minutes == highScore[0])
- {
- if (seconds > highScore[1])
- return true;
- }
-
- return false;
- }
-
- /**
- * Returns current state of the grid.
- */
-
- public byte[] getUserLevel()
- {
- return userLevel;
- }
-
-
- /**
- * Sets current state of the grid.
- */
-
- public void setUserLevel(byte[] userLevel)
- {
- this.userLevel = userLevel;
- for (int i = 0; i < userLevel.length; i++)
- {
- switch (userLevel[i])
- {
- case 1:
- cellsFound++;
- case 2: case 3:
- cellsToDraw++;
- }
- }
- }
-
- /**
- * Resets the level if user failed.
- */
-
- public void reset()
- {
- this.userLevel = new byte[this.level.length];
- cellsToDraw = cellsFound = 0;
- }
-
- /**
- * Handles a tap on the screen. This method determines in
- * which cell the tape was made. If no cell can be found
- * then we return false.
- * @param tool The tool currently in use
- * @param _x The x coordinate of the event
- * @param _y The y coordinate of the event
- */
-
- public boolean handlePenEvent(byte tool, int _x, int _y)
- {
- int col = (_x - x) / (CELL_WIDTH + 1);
- int row = (_y - y) / (CELL_WIDTH + 1);
-
- if (col >= 0 && col < cells &&
- row >= 0 && row < cells)
- {
- int index = row * cells + col;
-
- switch (tool)
- {
- case Tool.FILLING_TOOL:
- if (level[index] == 1)
- {
- if (userLevel[index] != 1)
- {
- userLevel[index] = 1;
- cellsToDraw++;
- cellsFound++;
- drawCell(index);
- parent.draw();
-
- if (cellsFound == hiddenCells)
- parent.win(level, levelName);
- }
- } else {
- return false;
- }
- break;
-
- case Tool.HELPER_TOOL:
- if (userLevel[index] != 1)
- {
- userLevel[index] = 2;
- cellsToDraw++;
- drawCell(index);
- parent.draw();
- }
- break;
-
- case Tool.ERASER_TOOL:
- switch (userLevel[index])
- {
- case 1:
- cellsFound--;
- case 2:
- cellsToDraw--;
- userLevel[index] = 3;
- drawCell(index);
- parent.draw();
- break;
- }
- break;
- }
- }
-
- return true;
- }
-
- /**
- * Paints the grid.
- */
-
- public void drawGrid()
- {
- g.setColor(0, 0, 0);
- g.drawRect(x, y, width, height);
-
- for (int i = 0; i < cells - 1; i++)
- {
- int _x = (x + 1) + (i + 1) * CELL_WIDTH + i;
- int _y = (y + 1) + (i + 1) * CELL_WIDTH + i;
-
- if ((i + 1) % 5 == 0)
- {
- g.drawDots(_x, y, _x, y + height - 1);
- g.drawDots(x, _y, x + width - 1, _y);
- } else {
- g.drawLine(_x, y, _x, y + height - 1);
- g.drawLine(x, _y, x + width - 1, _y);
- }
-
- rowsInfos[i].drawText(x - rowsInfos[i].getTextWidth(), _y - CELL_WIDTH, g);
- colsInfos[i].drawText(_x - CELL_WIDTH, y - colsInfos[i].getTextHeight(), g);
- }
-
- rowsInfos[cells - 1].drawText(x - rowsInfos[cells - 1].getTextWidth(),
- y + height - (CELL_WIDTH + 1), g);
- colsInfos[cells - 1].drawText(x + width - (CELL_WIDTH + 1),
- y - colsInfos[cells - 1].getTextHeight(), g);
- }
-
- /**
- * Draws the content of the cells.
- */
-
- public void drawCells()
- {
- if (cellsToDraw == 0)
- return;
-
- int drawnCells = 0;
-
- for (int i = 0; i < userLevel.length; i++)
- {
- if (drawCell(i))
- {
- if (++drawnCells == cellsToDraw)
- break;
- }
- }
- }
-
- // draws a single cell
-
- private boolean drawCell(int cell)
- {
- g.setColor(0, 0, 0);
- int col = cell % cells;
- int row = cell / cells;
-
- switch (userLevel[cell])
- {
- // filled cell
- case 1:
- g.fillRect((x + 1) + col * CELL_WIDTH + col, (y + 1) + row * CELL_WIDTH + row,
- CELL_WIDTH - 1, CELL_WIDTH - 1);
- return true;
-
- // checked cell
- case 2:
- int _x = (x + 1) + col * CELL_WIDTH + col;
- int _y = (y + 1) + row * CELL_WIDTH + row;
- g.drawLine(_x + 1, _y + 1, _x + CELL_WIDTH - 2, _y + CELL_WIDTH - 2);
- g.drawLine(_x + CELL_WIDTH - 2, _y + 1, _x + 1, _y + CELL_WIDTH - 2);
- return true;
-
- // erased cell
- case 3:
- g.setColor(255, 255, 255);
- g.fillRect((x + 1) + col * CELL_WIDTH + col, (y + 1) + row * CELL_WIDTH + row,
- CELL_WIDTH, CELL_WIDTH);
- return true;
- }
-
- return false;
- }
-
- // builds the informations displayed on top and left of the grid
-
- private void buildStrings()
- {
- rowsInfos = new MiniString[cells];
- colsInfos = new MiniString[cells];
-
- boolean filled;
- IntVector current;
- byte[] datas;
-
- // get rows informations
- for (int i = 0; i < cells; i++)
- {
- filled = false;
- current = new IntVector(7);
-
- for (int j = 0; j < cells; j++)
- {
- int _content = level[i * cells + j];
- if (_content == 0)
- continue;
- else
- filled = true;
-
- byte length = 0;
- while (_content == 1)
- {
- j++;
- length++;
- if (j == cells)
- break;
- _content = level[i * cells + j];
- }
-
- current.add(length);
- hiddenCells += length;
- }
-
- if (!filled)
- {
- datas = new byte[1];
- datas[0] = 0;
- } else {
- datas = new byte[current.getCount()];
- for (int k = 0; k < datas.length; k++)
- datas[k] = (byte) current.items[k];
- }
- rowsInfos[i] = new MiniString(datas, MiniString.HORIZONTAL);
- }
-
- // get columns informations
- for (int i = 0; i < cells; i++)
- {
- filled = false;
- current = new IntVector(7);
-
- for (int j = 0; j < cells; j++)
- {
- int _content = level[j * cells + i];
- if (_content == 0)
- continue;
- else
- filled = true;
-
- byte length = 0;
- while (_content == 1)
- {
- j++;
- length++;
- if (j == cells)
- break;
- _content = level[j * cells + i];
- }
-
- current.add(length);
- }
-
- if (!filled)
- {
- datas = new byte[1];
- datas[0] = 0;
- } else {
- datas = new byte[current.getCount()];
- for (int k = 0; k < datas.length; k++)
- datas[k] = (byte) current.items[k];
- }
- colsInfos[i] = new MiniString(datas, MiniString.VERTICAL);
- }
- }
- }
-
- // End of Grid.java
-